import kafka from 'kafka-node'

import kafkaConfig from '../config/kafka.js'

const { KafkaClient, Producer, Consumer, KeyedMessage }  = kafka

const client = new KafkaClient(kafkaConfig)
const producer = new Producer(client)

const km = new KeyedMessage('key', 'message')
const payloads = [
  { topic: 'topic1', messages: 'hi', partition: 0 },
  { topic: 'topic2', messages: ['hello', 'world', km] }
];

const consumer = new Consumer(client, [
  {
    topic: "topic1",
    partition: 0,
    offset: 0,
  },
  {
    topic: "topic2",
    partition: 0,
    offset: 0,
  }
]);

consumer.on('message', (message) => {
  console.log(message)
})

// producer.on('ready', function () {
//   producer.createTopics(['topic1', 'topic2'], () => {})

//   producer.send(payloads, function (err, data) {
//     if(err) console.error(err)
    
//     console.log(data);
//   });
// });

// producer.on('error', function (err) {})

client.close(() => console.log('关闭连接'))
